<?php
class rcmModel extends RPHP_Model
{

    public function getDocs($docid)
    {
        if (is_array($docid)) {
            $where = " WHERE `docid` IN (" . implode(",", array_unique($docid)) . ")";
        } else {
            $where = " WHERE `docid`=" . intval($docid);
        }

        $sql = "SELECT `docid`,`filename`,`create_time` FROM {$this->_prefix['primary']}documents " . $where;
        $rs  = $this->sql($sql);
        if ($rs) {
            foreach ($rs as $r) {
                $doc[$r['docid']] = $this->c['doc_host'] . date("Ymd", $r['create_time']) . "/" . date("H", $r['create_time']) . "/" . date("i", $r['create_time']) . "/" . $r['filename'];

            }
            return $doc;
        } else {
            return false;
        }

    }

    public function getCategory()
    {
        $sql = "SELECT `caid`,`name`,`fucaid`,`docid` FROM {$this->_prefix['primary']}goods_category WHERE `stat`=1 ORDER BY `fucaid` ASC,`sort` ASC,`caid` ASC";
        $tmp = $this->sql($sql);
        if ($tmp) {
            foreach ($tmp as $r) {


                $data[$r['fucaid']][] = $r;
            }

            return $data;
        }
    }

    public function getCateSon($caid=0)
    {
        $sql = "SELECT `caid`,`name`,`fucaid`,`docid` FROM {$this->_prefix['primary']}goods_category WHERE `stat`=1 AND `fucaid` = ".$caid." AND `fucaid` <> 0  ORDER BY `fucaid` ASC,`sort` ASC,`caid` ASC";
        $tmp = $this->sql($sql);
        $arr['data'] =[];
        $arr['count'] = 0;
        if($tmp){
            $doc =  array_column($tmp,"docid");
            $docids = $this->getDocs($doc);
            foreach($tmp as $k=>$v){
                $tmp[$k]['pic']  = "";
                if($v['docid']){

                    $tmp[$k]['pic'] = $docids[$v['docid']];
                }

            }

            $arr['data'] = $tmp;
            $arr['count'] = count($tmp);
        }

        return $arr;


    }

    public function getXimiMoney()
    {
        return $this->db->getone("SELECT `ximi_money` FROM {$this->_prefix['primary']}finance_balance WHERE `uid`=" . $this->_user['uid']);
    }

    public function getMemberLevel()
    {
        return $this->db->getrow("SELECT `member_level`,`m2` FROM {$this->_prefix['primary']}member_account WHERE `uid`=" . $this->_user['uid']);
    }

    public function getMember()
    {

        $sql = "SELECT * FROM {$this->_prefix['primary']}member_account WHERE  uid=" . $this->_user['uid'];
        return $this->db->getrow($sql);
    }

    public function getGoodsList($da, $page = 1, $pagesize = 20)
    {

        $where = " `is_on_sale`=1 AND `is_delete`=0";

        $caid = addslashes(htmlspecialchars(trim($da['caid'])));
        if (is_numeric($caid)) {
            $caid = intval($caid);
            $sql  = "SELECT `caid` FROM {$this->_prefix['primary']}goods_category WHERE `fucaid`=" . $caid . " AND `stat`=1";
            $tmp  = $this->sql($sql);
            if ($tmp) {
                $caids[] = $caid;
                foreach ($tmp as $r) {
                    $caids[] = $r['caid'];
                }

                $where .= " AND `caid` IN (" . implode(",", $caids) . ")";

            } else {
                $where .= " AND `caid`=" . $caid;
            }

        } else {
            if ($caid == "is_best") {
                $where .= " AND `is_best`=1";
            } elseif ($caid == "is_hot") {
                $where .= " AND `is_hot`=1";
            }
        }

        if ($da['pos'] < 0) {
            $start       = ($page - 1) * $pagesize;
            $newpagesize = $pagesize;
        } else {
            $start       = 0;
            $newpagesize = $page * $pagesize;
        }

        $sql = "SELECT COUNT(`goods_id`) AS `cnt` FROM {$this->_prefix['primary']}goods_data WHERE " . $where;
        $cnt = $this->db->getone($sql);
        if ($cnt > 0) {
            $orderby = "`weight` DESC,`shop_price` DESC";
            $limit   = " LIMIT " . $start . "," . $newpagesize;
            $sql     = "SELECT `goods_id`,`goods_name`,`thumb_docid`,`pic_docid`,`shop_price`,`market_price`,`sales_count`,`coupon_price`,`price_me`,`ximi_price` FROM {$this->_prefix['primary']}goods_data WHERE " . $where . " ORDER BY " . $orderby . $limit;

            $rs = $this->sql($sql);

            if ($rs) {
                foreach ($rs as $r) {

                    if ($r['thumb_docid'] > 0) {
                        $docids[] = $r['thumb_docid'];
                    } elseif ($r['pic_docid'] > 0) {
                        $docids[] = $r['pic_docid'];
                    }

                    $goods_ids[] = $r['goods_id'];

                }

                // 获取库存
                $sql = "SELECT `goods_id`,`number` FROM {$this->_prefix['primary']}goods_stock WHERE `goods_id` IN (" . implode(",", $goods_ids) . ")";
                $tmp = $this->sql($sql);

                if ($tmp) {
                    foreach ($tmp as $r) {
                        $goods_number[$r['goods_id']] = $r['number'];
                    }

                }

                if ($docids) {
                    $docs = $this->getDocs($docids);
                }

                foreach ($rs as $k => $r) {

                    if ($r['thumb_docid'] > 0 && $docs[$r['thumb_docid']]) {

                        $rs[$k]['thumb_docid'] = $docs[$r['thumb_docid']];

                    } elseif ($r['pic_docid'] > 0 && $docs[$r['pic_docid']]) {

                        $rs[$k]['thumb_docid'] = $docs[$r['pic_docid']];

                    }

                    if ($goods_number[$r['goods_id']] > 0) {
                        $rs[$k]['goods_number'] = $goods_number[$r['goods_id']];
                    } else {
                        $rs[$k]['goods_number'] = 0;
                    }

                }

            }
        }

        return array(
            'page'    => $page,
            'maxpage' => ceil($cnt / $pagesize),
            'count'   => $cnt,
            'data'    => $rs,
            'para'    => $para,
            'docs'    => $docs,
            'sql'     => $sql,
        );
    }
}
